package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/getMenu1")
    public Result getMenu1(String username) {
        try {
            List<Map<String, Object>> list = menuService.getMenuByUsername1(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);


        }
    }
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENULIST_SUCCESS,list);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_MENULIST_FAIL);
        }
    }
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu,Integer parentMenuId){
        try {
            menuService.add(menu,parentMenuId);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/findParentMenuIdByMenuId")
    public Result findParentMenuIdByMenuId(Integer menuId){
        try {
            Integer parentMenuId = menuService.findParentMenuIdByMenuId(menuId);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,parentMenuId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result edit(@RequestBody Menu menu,Integer parentMenuId){
        try {
            menuService.edit(menu,parentMenuId);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }
    @RequestMapping("/deleteById")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result deleteById(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }
}
